package com.znyj.cloud.thrift;

import com.znyj.cloud.chain.util.ByteUtil;
import org.apache.log4j.Logger;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.ServerContext;
import org.apache.thrift.server.TServerEventHandler;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

/**
 * Created by gaoliwei02 on 2017/10/29.
 */
public class MyTServerEventHandler implements TServerEventHandler {
    private static final Logger logger = Logger.getLogger(MyTServerEventHandler.class);


    @Override
    public void preServe() {
        logger.info("@@@@ preServe ...");
    }

    /**
     * 创建Context的时候，触发
     * 在server启动后，只会执行一次
     */
    public ServerContext createContext(TProtocol input, TProtocol output) {
        logger.info("@@@@ createContext");
        return null;
    }

    /**
     * 删除Context的时候，触发
     * 在server启动后，只会执行一次
     */
    @Override
    public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) {
        logger.info("@@@@ deleteContext");
    }

    /**
     * 调用RPC服务的时候触发
     * 每调用一次方法，就会触发一次
     */
    @Override
    public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport) {
        /**
         * 把TTransport对象转换成TSocket，然后在TSocket里面获取Socket，就可以拿到客户端IP
         */
        //TSocket socket = (TSocket) inputTransport;
        //logger.info("@@@@@@@@@@@@@@@@ ip:" + socket.getSocket().getRemoteSocketAddress());
        //String ss = ByteUtil.byte2String();
       logger.info("@@@@ processContext");
    }
}
